package com.aurora.base.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class FingerprintUtil {

	Map<String, Object> keyMap = new HashMap<String, Object>();
	  public static void main(String[] args) {
		  String data = FingerprintUtil.fingerPrintEncrypt("oqryos16gzUjK2PMi_wkFHIj-a-Y", BigDecimal.valueOf(100000000), BigDecimal.valueOf(100000000),BigDecimal.valueOf(100000000),BigDecimal.valueOf(0));
		  System.out.println(data);
//		 System.out.println(FingerprintUtil.fingerPrintDecrypt(data,"1",BigDecimal.ONE, BigDecimal.ONE,BigDecimal.ONE));
	  }
	  
	
	/**
	 * 校验金额加密算法
	 * @param openId
	 * @param balance
	 * @param balanceHistory
	 * @param sumBalance
	 * @return
	 */
	public static String fingerPrintEncrypt(String openId,BigDecimal balance,BigDecimal balanceHistory,BigDecimal sumBalance,BigDecimal freezeBalance){
		try{
			DecimalFormat df = new DecimalFormat("0.00");
			String checkOut = df.format(balance.add(balanceHistory.add(sumBalance.add(freezeBalance))).doubleValue());
			checkOut = MD5Check.getMD5String(openId+checkOut);
			String publicKey = RsaUtil.getPublicKey();
//			System.out.println(publicKey);
			byte[] fingerprint = RsaUtil.encryptByPublicKey(checkOut.getBytes(),publicKey);
//			System.out.println("fingerprintlength:"+Arrays.toString(fingerprint));
			return bytesToHexString(fingerprint);
		}catch (Exception e) {
			// TODO: handle exception
			System.out.println("fingerPrintEncrypt异常:"+e.getMessage());
			return "";
		}
	}
	
	/**
	 * 已加密的数据解密并校验算法
	 * @param data
	 * @param openId
	 * @param balance
	 * @param balanceHistory
	 * @param sumBalance
	 * @return
	 */
	public static boolean fingerPrintDecrypt(String data,String openId,BigDecimal balance,BigDecimal balanceHistory,BigDecimal sumBalance,BigDecimal freezeBalance){
		try{
			DecimalFormat df = new DecimalFormat("0.00");
			String checkOut = df.format(balance.add(balanceHistory.add(sumBalance.add(freezeBalance))).doubleValue());
			checkOut = MD5Check.getMD5String(openId+checkOut);
			String privateKey = RsaUtil.getPrivateKey();
			System.out.println("fingerPrintDeclength:"+Arrays.toString(hexStringToByte(data)));
			String tempCheckOut = new String(RsaUtil.decryptByPrivateKey(hexStringToByte(data), privateKey));
			if(tempCheckOut.equals(checkOut)){
				return true;
			}
			return false;
		}catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
			return false;
		}
	}
	
	
	
	public static final String bytesToHexString(byte[] bArray) {
		  StringBuffer sb = new StringBuffer(bArray.length);
		  String sTemp;
		  for (int i = 0; i < bArray.length; i++) {
			   sTemp = Integer.toHexString(0xFF & bArray[i]);
			   if (sTemp.length() < 2)
			   sb.append(0);
			   sb.append(sTemp.toUpperCase());
		  }
		  return sb.toString();
	 }
	 
	 /**
	   * 把16进制字符串转换成字节数组
	   * @param hexString
	   * @return byte[]
	   */
	  public static byte[] hexStringToByte(String hex) {
		   int len = (hex.length() / 2);
		   byte[] result = new byte[len];
		   char[] achar = hex.toCharArray();
		   for (int i = 0; i < len; i++) {
		    int pos = i * 2;
		    result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		   }
		   return result;
	  }
	  
	  private static int toByte(char c) {
		    byte b = (byte)"0123456789ABCDEF".indexOf(c);
		    return b;
	  }
	
}
